package pms.dao;

import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import pms.dto.EventTheaterDTO;
import pms.om.Event;
import pms.om.Theater;

/**
 * The Data access class for events. All Interaction with the database
 * regarding the entity bean Event should be handled by this class!
 * @author KRS DEV Team (krsdevteam@PMSWIbb2011SS.de)
 */
public class EventDao extends HibernateDaoSupport {
	private TheaterDao theaterDao;
	
	public void setTheaterDao(TheaterDao theaterDao) {
		this.theaterDao = theaterDao;
	}

	/**
	 * returns a single event by its primary db key
	 * @param id reference to event
	 * @return a single Event
	 */
	public Event findById(int id) {
		HibernateTemplate template = getHibernateTemplate();
		return (Event) template.get(Event.class, id);
	}

	/**
	 * Returns all Events from the database.
	 * @return a list of Event
	 * @see Event
	 */
	@SuppressWarnings("unchecked")
	public List<Event> findAll() {
		HibernateTemplate template = getHibernateTemplate();
		return template.loadAll(Event.class);
	}
	
	/**
	 * modifies an existing event in the database which is selected by its primary key.
	 * @param id the primary key of the boat
	 * @param name the new name for the event
	 * @param datum the new datum for the event
	 * @param zeit the new zeit for the event
	 * @param preis_loge the new preis_loge for the event
	 * @param preis_reg the new preis_reg for the event
	 * @param theaterID reference to the theater
	 * @see Event
	 */
	public void modify(int id, String name, String datum, String zeit, String preis_loge, String preis_reg, int theaterId) {
		HibernateTemplate template = getHibernateTemplate();
		Event event = (Event) template.get(Event.class, id);
		event.setName(name);
		event.setDatum(datum);
		event.setZeit(zeit);
		event.setPreis_loge(preis_loge);
		event.setPreis_reg(preis_reg);
		event.setTheater(theaterDao.findById(theaterId));
		
		template.saveOrUpdate(event);
	}
	
	/**
	 * modifies number of seats of an existing event in the database which is selected by its primary key.
	 * @param id the primary key of the boat
	 * @param i1 the number of seats loge
	 * @param i2 the number of seats regular
	 * @see Event
	 */
	public void modifyPlaetze(int id, int i1, int i2) {
		HibernateTemplate template = getHibernateTemplate();
		Event event = (Event) template.get(Event.class, id);
		event.setPlaetze_loge(i1);
		event.setPlaetze_reg(i2);
		
		template.saveOrUpdate(event);
	}

	/**
	 * creates a new persistent Event in the database providing all the necessary details.
	 * @param name the new name for the event
	 * @param datum the new datum for the event
	 * @param zeit the new zeit for the event
	 * @param preis_loge the new preis_loge for the event
	 * @param preis_reg the new preis_reg for the event
	 * @param theaterID reference to the theater
	 * @see Event
	 */
	public void create(String name, String datum, String zeit, String preis_loge, String preis_reg, int theaterId) {
		HibernateTemplate template = getHibernateTemplate();
		Event event = new Event(name, datum, zeit, preis_loge, preis_reg, theaterDao.findById(theaterId),theaterDao.findById(theaterId).getPlaetze_loge(),theaterDao.findById(theaterId).getPlaetze_reg());
		template.persist(event);
	}
	

	@SuppressWarnings("unchecked")
	public List<EventTheaterDTO> findAllET() {
		HibernateTemplate template = getHibernateTemplate();
		return (List<EventTheaterDTO>) template.execute(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				Criteria criteria = session.createCriteria(Event.class);
				List<Event> alleEvents = criteria.list();
				List<EventTheaterDTO> liste = new LinkedList<EventTheaterDTO>();
				for (Event e : alleEvents) {
					Theater t = e.getTheater();
					EventTheaterDTO etDto = new EventTheaterDTO(e, t);
					liste.add(etDto);
					
				}
				return liste;
			}
		});
	}
	
		
}